home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_300
/
311_01
/
db_main.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-04-21
|
48KB
|
1,936 lines
/****************************************************************************/
/* */
/* */
/* db_main.c v1.3 (c) 1987-1990 Ken Harris */
/* */
/* */
/****************************************************************************/
/* */
/* This software is made available on an AS-IS basis. Unrestricted */
/* use is granted provided that the copyright notice remains intact. */
/* The author makes no warranties expressed or implied. */
/* */
/****************************************************************************/
#include "dblib.h"
static char *copyright = "db V1.3 (c) 1987-1990 Ken Harris";
int db_error = 0;
ulong db_match_blk = 0;
ushort db_match_rec = 0;
ulong db_add_blk = 0;
ushort db_add_rec = 0;
/*
* db_create - Create a New Data File
*/
DATA_FILE db_create(path, fname, options)
char *path, *fname, *options;
{
BUFFER db_alloc_buf();
DATA_FILE df;
FILE_HDR fhdr;
char *fname_dflts(), *calloc();
db_error = 0;
df = (DATA_FILE) calloc(1, sizeof(struct db_data_file));
if (!df)
{ db_error = DB_ALLOC_ERROR;
return(NULL);
}
df->df_fhdr = db_alloc_buf(DB_FILE_HDR_SIZE);
fhdr = (FILE_HDR) df->df_fhdr->buf_data;
if (db_error) { db_free_df(df); return(NULL); }
df->df_fhdr->buf_cur_size = DB_FILE_HDR_SIZE;
strcpy(df->df_fname, fname_dflts(fname,path));
db_file_options(fhdr, options);
if (db_error) { db_free_df(df); return(NULL); }
db_fhdr_create(fhdr);
if (db_error) { db_free_df(df); return(NULL); }
#ifdef MSC
df->df_fd = open(df->df_fname, O_CREAT|O_TRUNC|O_RDWR|O_BINARY, S_IREAD|S_IWRITE);
#endif
#ifdef TURBO
df->df_fd = open(df->df_fname, O_CREAT|O_TRUNC|O_RDWR|O_BINARY, S_IREAD|S_IWRITE);
#endif
#ifdef ULTRIX
df->df_fd = open(df->df_fname, O_CREAT|O_TRUNC|O_RDWR, S_IREAD|S_IWRITE);
#endif
#ifdef SYSV
df->df_fd = open(df->df_fname, O_CREAT|O_TRUNC|O_RDWR, S_IREAD|S_IWRITE);
#endif
if (df->df_fd < 0)
{ db_error = DB_FILE_NOT_CREATED;
db_free_df(df);
return(NULL);
}
df->df_stat = DB_OPEN;
switch (fhdr->fh_file_type)
{
#ifdef IDX
case DB_INDEX:
df->df_buf = db_alloc_buf(fhdr->fh_block_size+2*fhdr->fh_rec_size);
if (db_error) { close(df->df_fd); db_free_df(df); return(NULL); }
df->df_tmp = db_alloc_buf(fhdr->fh_block_size+2*fhdr->fh_rec_size);
if (db_error) { close(df->df_fd); db_free_df(df); return(NULL); }
df->df_aux = db_alloc_buf(fhdr->fh_block_size+2*fhdr->fh_rec_size);
if (db_error) { close(df->df_fd); db_free_df(df); return(NULL); }
df->df_buf->buf_size = fhdr->fh_block_size;
df->df_tmp->buf_size = fhdr->fh_block_size;
df->df_aux->buf_size = fhdr->fh_block_size;
break;
#endif
#ifdef RAN
case DB_RANDOM:
df->df_buf = db_alloc_buf(fhdr->fh_block_size);
if (db_error) { close(df->df_fd); db_free_df(df); return(NULL); }
df->df_tmp = db_alloc_buf(fhdr->fh_block_size);
if (db_error) { close(df->df_fd); db_free_df(df); return(NULL); }
while (fhdr->fh_last_block < fhdr->fh_base_size)
{ db_extend(df, df->df_buf);
if (db_error) { close(df->df_fd); db_free_df(df); return(NULL); }
db_put_blk(df, df->df_buf);
}
break;
#endif
#ifdef SEQ
case DB_SEQ:
df->df_buf = db_alloc_buf(fhdr->fh_block_size);
if (db_error) { close(df->df_fd); db_free_df(df); return(NULL); }
break;
#endif
#ifdef VAR
case DB_VAR:
df->df_buf = db_alloc_buf(fhdr->fh_block_size);
if (db_error) { close(df->df_fd); db_free_df(df); return(NULL); }
break;
#endif
}
db_put_blk(df, df->df_fhdr);
if (db_error) { close(df->df_fd); db_free_df(df); return(NULL); }
return(df);
}
/*
* db_file_options - parse and setup file options
*/
#define OPTION_SIZE 20
#define SUB_OPTION_SIZE 50
db_file_options(fh, option_str)
FILE_HDR fh;
char *option_str;
{
char option[OPTION_SIZE], sub_option[SUB_OPTION_SIZE];
char *c, *o_ptr, *s_ptr;
int cnt;
c = option_str;
while (*c)
{ while (*c && !isalpha(*c)) c++;
o_ptr = option;
s_ptr = sub_option;
*o_ptr = *s_ptr = NULL;
for (cnt=0; isalpha(*c); cnt++, c++)
{ if (cnt < OPTION_SIZE)
{ if (islower(*c))
*o_ptr++ = toupper(*c);
else
*o_ptr++ = *c;
}
}
while (*c && *c != '=' && *c != ',') c++;
if (*c == '=')
{ c++;
while (*c && *c <= ' ') c++;
for (cnt=0; *c && *c!=',' && !isspace(*c); cnt++, c++)
{ if (cnt < SUB_OPTION_SIZE)
*s_ptr++ = *c;
}
while (*c && *c != ',') c++;
}
*o_ptr++ = NULL;
*s_ptr++ = NULL;
db_file_1option(fh, option, sub_option);
if (db_error) return;
}
}
/*
* db_file_1option - Process one file option
*/
db_file_1option(fh, option, sub_option)
FILE_HDR fh;
char *option, *sub_option;
{
if (strcmp(option, "SEQ")==0)
{ if (fh->fh_file_type == 0)
fh->fh_file_type = DB_SEQ;
else
db_error = DB_FTYPE_ERROR;
}
else
if (strcmp(option, "RAN")==0)
{ if (fh->fh_file_type == 0)
fh->fh_file_type = DB_RANDOM;
else
db_error = DB_FTYPE_ERROR;
}
else
if (strcmp(option, "IDX")==0)
{ if (fh->fh_file_type == 0)
fh->fh_file_type = DB_INDEX;
else
db_error = DB_FTYPE_ERROR;
}
else
if (strcmp(option, "VAR")==0)
{ if (fh->fh_file_type == 0)
fh->fh_file_type = DB_VAR;
else
db_error = DB_FTYPE_ERROR;
}
else
if (strcmp(option, "BLK")==0)
fh->fh_block_size = atoi(sub_option);
else
if (strcmp(option, "REC")==0)
fh->fh_data_size = atoi(sub_option);
else
if (strcmp(option, "BASE")==0)
fh->fh_base_size = atoi(sub_option);
else
if (strcmp(option, "KEY")==0)
fh->fh_key_size = atoi(sub_option);
else
if (strcmp(option, "ATR")==0)
fh->fh_atr_size = atoi(sub_option);
else
if (strcmp(option, "MLINKS")==0)
fh->fh_mlink_cnt = atoi(sub_option);
else
if (strcmp(option, "OLINKS")==0)
fh->fh_olink_cnt = atoi(sub_option);
else
if (strcmp(option, "KLINKS")==0)
fh->fh_klink_cnt = atoi(sub_option);
else
if (strcmp(option, "DUPS")==0)
fh->fh_file_stat &= ~DB_DUP_ALLOWED;
else
if (strcmp(option, "NODUPS")==0)
fh->fh_file_stat |= DB_DUP_ALLOWED;
else
db_error = DB_INVALID_OPTION;
}
/*
* db_fhdr_create - Check file header data on create
*/
void db_fhdr_create(fh)
FILE_HDR fh;
{ short hdr_size;
db_error = 0;
fh->fh_db_version = DB_VERSION;
if (fh->fh_file_type != DB_SEQ &&
fh->fh_file_type != DB_RANDOM &&
fh->fh_file_type != DB_INDEX &&
fh->fh_file_type != DB_VAR)
{ db_error = DB_INVALID_FHDR;
return;
}
#ifndef SEQ
if (fh->fh_file_type == DB_SEQ)
{ db_error = DB_UNSUPP_FEATURE;
return;
}
#endif
#ifndef RAN
if (fh->fh_file_type == DB_RANDOM)
{ db_error = DB_UNSUPP_FEATURE;
return;
}
#endif
#ifndef IDX
if (fh->fh_file_type == DB_INDEX)
{ db_error = DB_UNSUPP_FEATURE;
return;
}
#endif
#ifndef VAR
if (fh->fh_file_type == DB_VAR)
{ db_error = DB_UNSUP